package com.reform.service.impl;

import com.reform.pojo.common.Record;
import com.reform.mapper.RecordMapper;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reform.utils.VerifycUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-08-05
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {

    @Autowired
    RecordMapper recordMapper;

    @Override
    public Result creatRecord(Record record) {
        if (recordMapper.insert(record) > 0)
            return Result.success(ResultCode.SUCCESS);
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public Result getAllRecord() {
        return Result.success(ResultCode.SUCCESS, recordMapper.selectList(null));
    }

    @Override
    public Result getRecordByProId(String proId) {
        Map<String, Object> map = new HashMap<>();
        map.put("pro_id", proId);
        return Result.success(ResultCode.SUCCESS, recordMapper.selectByMap(map));
    }

    @Override
    public Result getRecordByProIdOrGH(String proId, String gh) {
        Map<String, Object> map = new HashMap<>();
        if (VerifycUtils.verifyNullOrEmptyUtils(proId)) {
            map.put("pro_id", proId);
        }
        if (VerifycUtils.verifyNullOrEmptyUtils(gh)) {
            map.put("gh", gh);
        }
        if (map.isEmpty()) {
            return Result.failure(ResultCode.PARAM_IS_MISSING);
        }
        return Result.success(ResultCode.SUCCESS, recordMapper.selectByMap(map));
    }
}
